var smcInstance;
var displayMode:Object;
var dynamicPortletArr:Object;

var nowListeningResult:Object;
var nowRecordingResult:Object;
var nowExpectingResult:Object;
var nowActingResult:Object;

var bookPageNum:Number;
var bookPageTotal:Number;
var currentSearchType:String;

function init(param, data, slot)
{
	Packages.log(this+".init...");

	Packages.model.Context.activeContent=this;

	// Param.init
	smcInstance=mlf.mcmanager.SMCManager.getInstance(slot);
	displayMode=new Object();
	displayMode["nowListeningPanel"]="normal";
	displayMode["nowRecordingPanel"]="normal";
	displayMode["nowExpectingPanel"]="normal";
	displayMode["nowActingPanel"]="normal";

	dynamicPortletArr=new Object();
	
	// UI.init
	var basic=Packages.system.mlf.GateKeeper.scanElement(this._name, "basic");
	Packages.system.display.JAccordion.initialize(basic);
	var expandPortletArr:Array=new Array();
	expandPortletArr.push(basic["nowListeningPanel"]);
	expandPortletArr.push(basic["nowRecordingPanel"]);
	expandPortletArr.push(basic["nowExpectingPanel"]);
	expandPortletArr.push(basic["nowActingPanel"]);
	var styleObj:Object=new Object();
	styleObj.mainLocation=new Object();
	styleObj.subLocation=new Object();
	styleObj.mainLocation.x=0;
	styleObj.mainLocation.y=10;
	styleObj.mainLocation.width=500;
	styleObj.mainLocation.height=500;
	styleObj.subLocation.bx=510;
	styleObj.subLocation.by=10;
	styleObj.subLocation.dx=0;
	styleObj.subLocation.dy=40;
	styleObj.subLocation.width=160;
	styleObj.subLocation.height=40;
	Packages.system.display.JAccordion.setExpandPortletArr(expandPortletArr, styleObj);

	// Render.init
	Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "homePageTitle"));
	Packages.system.display.SceneTransition.rollOut();

	// Event.init

	invokeInitComplete();
}

function dispose()
{
	Packages.system.mlf.GateKeeper.removeListener("onJAccodionCellEvent", this);
	Packages.system.mlf.GateKeeper.removeListener("onReleaseButtonEvent", this);
}

function invokeInitComplete()
{
	/*Packages.modula.RequestCommand.request("HTTP", "getCurrentListenningBookList", null, this, Packages.model.vo.BookListView, "normal");
	Packages.modula.RequestCommand.request("HTTP", "getCurrentRecordingBookList", null, this, Packages.model.vo.BookListView, "normal");
	Packages.modula.RequestCommand.request("HTTP", "getCurrentExpectingBookList", null, this, Packages.model.vo.BookListView, "normal");
	Packages.modula.RequestCommand.request("HTTP", "getCurrentUserActivityList", null, this, Packages.model.vo.ActivityListView, "normal");*/

	// Packages.modula.RequestCommand.request("HTTP", "members/2", null, Packages.model.vo.UserView, "normal", this);
	// Packages.modula.RequestCommand.request("HTTP", "members/2/friends", null, Packages.model.vo.UserListView, "normal", this);

	Packages.system.mlf.GateKeeper.addListener("onJAccodionCellEvent", this);
	Packages.system.mlf.GateKeeper.addListener("onReleaseButtonEvent", this);

	loadNowListeningList();
	loadNowRecordingList();
	loadNowExpectingList();
	// loadNowActingList();

	bookPageNum=1;
}

function onJAccodionCellEvent(evt:Object)
{
	// Packages.log(evt.type+": "+evt.param);

	// neither current mode nor previous mode is expand, then quit
	if (evt.data[evt.param]!="expand" && displayMode[evt.param]!="expand") return;
	
	if (evt.param=="nowListeningPanel") 
	{
		displayMode.nowListeningPanel=evt.data.nowListeningPanel;
		if (dynamicPortletArr.nowListeningPanel=="nowListening")
		{
			loadNowListeningList();
		}
	}
	else if (evt.param=="nowRecordingPanel") 
	{
		displayMode.nowRecordingPanel=evt.data.nowRecordingPanel;
		
		if (dynamicPortletArr.nowRecordingPanel=="nowRecording")
		{
			loadNowRecordingList();
		}
	}
	else if (evt.param=="nowExpectingPanel") 
	{
		displayMode.nowExpectingPanel=evt.data.nowExpectingPanel;

		if (dynamicPortletArr.nowExpectingPanel=="nowExpecting")
		{
			loadNowExpectingList();
		}
	}
	else if (evt.param=="nowActingPanel") 
	{
		displayMode.nowActingPanel=evt.data.nowActingPanel;

		if (dynamicPortletArr.nowActingPanel=="nowActing")
		{
			loadNowActingList();
		}
	}
}

function loadList(param, data)
{
	if (currentSearchType=="Listening") loadNowListeningList(null, data);
	else if (currentSearchType=="Recording") loadNowRecordingList(null, data);
	else if (currentSearchType=="Expecting") loadNowExpectingList(null, data);
	else if (currentSearchType=="Acting") loadNowActingList(null, data);
}

function previousBookList()
{
	if (bookPageNum>1)
	{
		bookPageNum--;
		loadList(currentSearchType, {pageNum:bookPageNum});
	}
}

function nextBookList()
{
	if (bookPageNum<bookPageTotal)
	{
		bookPageNum++;
		loadList(currentSearchType, {pageNum:bookPageNum});
	}
}

function loadNowListeningList(param, data)
{
	dynamicPortletArr.nowListeningPanel="nowListening";
	Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "nowListening"));
	if (displayMode.nowListeningPanel=="normal")
	{
		Packages.modula.RequestCommand.request("HTTP", "restlet/books/latest?startIndex=0&count=6", null, Packages.model.vo.BookListView, displayMode.nowListeningPanel, this, {container:"nowListening", slot:this._name, uri:"app/content/HomePage.xml", dynamicPortlet:dynamicPortletArr.nowListeningPanel, saved:"nowListeningResult", initMethod:"lib:notes.components.content.HomePage.onListeningInit"});	// @tmp request
	}
	else 
	{
		//
		if (data.pageNum==null)
		{
			bookPageNum=1;
		}
		else bookPageNum=data.pageNum;

		startIndex=(bookPageNum-1)*15;

		Packages.modula.RequestCommand.request("HTTP", "restlet/books/latest?startIndex="+startIndex+"&count=15", null, Packages.model.vo.BookListView, displayMode.nowListeningPanel, this, {container:"nowListening", slot:this._name, uri:"app/content/HomePage.xml", dynamicPortlet:dynamicPortletArr.nowListeningPanel, saved:"nowListeningResult", initMethod:"lib:notes.components.content.HomePage.onListeningInit", enterEvent:"loadNowListeningList"});	// @tmp request
	}
}

function loadNowRecordingList(param, data)
{
	dynamicPortletArr.nowRecordingPanel="nowRecording";
	Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "nowRecording"));
	if (displayMode.nowRecordingPanel=="normal")
	{
		Packages.modula.RequestCommand.request("HTTP", "restlet/books/latest?startIndex=0&count=6", null, Packages.model.vo.BookListView, displayMode.nowRecordingPanel, this, {container:"nowRecording", slot:this._name, uri:"app/content/HomePage.xml", dynamicPortlet:dynamicPortletArr.nowRecordingPanel, saved:"nowRecordingResult", initMethod:"lib:notes.components.content.HomePage.onRecordingInit"});	// @tmp request
	}
	else 
	{
		//
		if (data.pageNum==null)
		{
			bookPageNum=1;
		}
		else bookPageNum=data.pageNum;

		startIndex=(bookPageNum-1)*15;

		Packages.modula.RequestCommand.request("HTTP", "restlet/books/latest?startIndex="+startIndex+"&count=15", null, Packages.model.vo.BookListView, displayMode.nowRecordingPanel, this, {container:"nowRecording", slot:this._name, uri:"app/content/HomePage.xml", dynamicPortlet:dynamicPortletArr.nowRecordingPanel, saved:"nowRecordingResult", initMethod:"lib:notes.components.content.HomePage.onRecordingInit", enterEvent:"loadNowRecordingList"});	// @tmp request
	}
}

function loadNowExpectingList(param, data)
{
	dynamicPortletArr.nowExpectingPanel="nowExpecting";
	Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "nowExpecting"));
	if (displayMode.nowExpectingPanel=="normal")
	{
		Packages.modula.RequestCommand.request("HTTP", "restlet/books/latest?startIndex=0&count=6", null, Packages.model.vo.BookListView, displayMode.nowExpectingPanel, this, {container:"nowExpecting", slot:this._name, uri:"app/content/HomePage.xml", dynamicPortlet:dynamicPortletArr.nowExpectingPanel, saved:"nowExpectingResult", initMethod:"lib:notes.components.content.HomePage.onExpectingInit"});	// @tmp request
	}
	else 
	{
		//
		if (data.pageNum==null)
		{
			bookPageNum=1;
		}
		else bookPageNum=data.pageNum;

		startIndex=(bookPageNum-1)*15;

		Packages.modula.RequestCommand.request("HTTP", "restlet/books/latest?startIndex="+startIndex+"&count=15", null, Packages.model.vo.BookListView, displayMode.nowExpectingPanel, this, {container:"nowExpecting", slot:this._name, uri:"app/content/HomePage.xml", dynamicPortlet:dynamicPortletArr.nowExpectingPanel, saved:"nowExpectingResult", initMethod:"lib:notes.components.content.HomePage.onExpectingInit", enterEvent:"loadNowExpectingList"});	// @tmp request
	}
}

function loadActingList(param, data)
{
	dynamicPortletArr.nowActingPanel="nowActing";
	Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "nowActing"));
	if (displayMode.nowActingPanel=="normal")
	{
		Packages.modula.RequestCommand.request("HTTP", "restlet/books/latest?startIndex=0&count=6", null, Packages.model.vo.BookListView, displayMode.nowActingPanel, this, {container:"nowActing", slot:this._name, uri:"app/content/HomePage.xml", dynamicPortlet:dynamicPortletArr.nowActingPanel, saved:"nowActingResult", initMethod:"lib:notes.components.content.HomePage.onActingInit"});	// @tmp request
	}
	else 
	{
		//
		if (data.pageNum==null)
		{
			bookPageNum=1;
		}
		else bookPageNum=data.pageNum;

		startIndex=(bookPageNum-1)*15;

		Packages.modula.RequestCommand.request("HTTP", "restlet/books/latest?startIndex="+startIndex+"&count=15", null, Packages.model.vo.BookListView, displayMode.nowActingPanel, this, {container:"nowActing", slot:this._name, uri:"app/content/HomePage.xml", dynamicPortlet:dynamicPortletArr.nowActingPanel, saved:"nowActingResult", initMethod:"lib:notes.components.content.HomePage.onActingInit", enterEvent:"loadNowActingList"});	// @tmp request
	}
}

function onListeningInit()
{
	currentSearchType="Listening";
	var containermc=Packages.system.mlf.GateKeeper.scanElement(this._name, dynamicPortletArr.nowListeningPanel);
	adjustLayout(displayMode.nowListeningPanel, containermc);
	onCoverInit(displayMode.nowListeningPanel, "nowListening");
}

function onRecordingInit()
{
	currentSearchType="Recording";
	var containermc=Packages.system.mlf.GateKeeper.scanElement(this._name, dynamicPortletArr.nowRecordingPanel);
	adjustLayout(displayMode.nowRecordingPanel, containermc);
	onCoverInit(displayMode.nowRecordingPanel, "nowRecording");
}

function onExpectingInit()
{
	currentSearchType="Listening";
	var containermc=Packages.system.mlf.GateKeeper.scanElement(this._name, dynamicPortletArr.nowExpectingPanel);
	adjustLayout(displayMode.nowExpectingPanel, containermc);
	onCoverInit(displayMode.nowExpectingPanel, "nowExpecting");
}

function onActingInit()
{
	currentSearchType="Listening";
	var containermc=Packages.system.mlf.GateKeeper.scanElement(this._name, dynamicPortletArr.nowActingPanel);
	adjustLayout(displayMode.nowActingPanel, containermc);
	onCoverInit(displayMode.nowActingPanel, "nowActing");
}

function adjustLayout(mode:String, containermc:MovieClip)
{
	var height:Number;
	var width:Number;
	var evtTargetArr:Array=new Array();

	var rst=this[containermc._name+"Result"];

	if (mode=="normal")
	{
		height=215;
		width=230;
		evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "moreSpot_nowListening"));
		evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "moreSpot_nowRecording"));
		evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "moreSpot_nowExpecting"));
		evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "moreSpot_nowActing"));
	}
	else if (mode=="expand")
	{
		height=460;
		width=500;
		evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListPreSpot"));
		evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListNextSpot"));
		
		var bookListTotalPage=Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListTotalPage");
		bookPageTotal=Math.floor(rst.count/15)+1;
		bookListTotalPage.text=bookPageTotal;

		var bookListCurrentPage=Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListCurrentPage");
		bookListCurrentPage.text=bookPageNum;
		var tf_fmt:TextFormat=new TextFormat();
		tf_fmt.align="right";
		bookListCurrentPage.setTextFormat(tf_fmt);
	}

	var param:String="area";
	param=param+":"+height+":"+width+":noSlider";
	smcInstance.updateStyle("layout", containermc, param);
	smcInstance.applyStyle();

	Packages.system.events.StateFulButtonEvent.initialize(evtTargetArr);

	Packages.system.render.Transition.fadeType1(containermc);
}

function onCoverInit(mode:String, container:String)
{
	var processBookId;
	var coverContainer;
	var minWidth:Number;
	var minHeight:Number;

	var rst=this[container+"Result"];

	if (mode=="normal")
	{
		minHeight=90;
		minWidth=72;
	}
	else if (mode=="expand")
	{
		minHeight=110;
		minWidth=80;
	}

	for (var i=0; i<rst.books.length; i++)
	{
		processBookId=rst.books[i].bookId;
		coverContainer=Packages.system.mlf.GateKeeper.scanElement(this._name, "bookItem_"+processBookId+"_"+container);
		var listener:Object=new Object();
		listener.onLoadInit=function(target_mc)
		{
			var zoom=Packages.system.utils.ImageUtil.minSize(target_mc, minWidth, minHeight);
			target_mc._xscale=zoom;
			target_mc._yscale=zoom;
			// centralize the target
			var offsetX:Number=(target_mc._width-minWidth)/2;
			target_mc._x=-offsetX;
			var offsetY:Number=(target_mc._height-minHeight)/2;
			target_mc._y=-offsetY;
		}
		mlf.mcmanager.dynamicloading.LoadApp.loadAppLoader(rst.books[i].image, coverContainer, "img", listener); 
	}
}

function expandMore(container:String)
{
	var cellPortlet=Packages.system.mlf.GateKeeper.scanElement(this._name, container+"Panel");
		
	Packages.system.display.JAccordion.invokeReleaseEvent(mlf.mcmanager.PathResolver.drawPath(cellPortlet.header));
}

function onReleaseButtonEvent(evt:Object)
{
	if (evt.param.indexOf("bookItem")>-1)
	{
		var eleArr:Array=evt.param.split("_");
		var selectedBookId=eleArr[1];
		var selectedContainer=eleArr[2];
		if (selectedContainer!=null && selectedBookId!=null)
		{
			Packages.system.utils.ComponentsUtil.popupSelectedDataDetail(this[selectedContainer+"Result"], selectedBookId, "BookDetail", "content/bookPage/BookSpecial");
		}
	}
	else if (evt.param=="bookListPreSpot")
	{
		previousBookList();
	}
	else if (evt.param=="bookListNextSpot")
	{
		nextBookList();
	}
	else if (evt.param.indexOf("moreSpot")>-1)
	{
		var container=evt.param.substr(9);
		expandMore(container);
	}
}

function handleResponseResult(rsp:Object)
{
	var dynamicPortlet=Packages.system.mlf.GateKeeper.scanElement(this._name, rsp.component.data.dynamicPortlet);
	if (dynamicPortlet!=null)
	{
		for (var a in dynamicPortlet)
		{
			if (typeof(dynamicPortlet[a])=="movieclip")
			{
				dynamicPortlet[a].removeMovieClip();
			}
		}
		Packages.system.mlf.GateKeeper.loadML2byContent(rsp.vo.instance.content);
	}
	else Packages.system.mlf.GateKeeper.loadML2byContent(rsp.vo.instance.content);

	if (rsp.component.data.saved!=null)
	{
		this[rsp.component.data.saved]=rsp.service.result;
	}

	Packages.system.display.Progress.removeInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, rsp.component.data.container));
}